package com.second.app.superthread;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @Author GYZ
 * @Date 2024/1/23
 * <pre>
 *   await(long timeOut , TimeUnit unit) 功能是如果在指定的时间内达到parties的数量
 *        则程序继续向下运行，如果出现超时，则抛出TimeoutException
 *   getNumberWaiting()： 在屏障处等待线程的个数
 *   getParties*()：获取屏障对象的parties的个数
 * </pre>
 */


class ColdService{

    public CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
        @Override
        public void run() {
            System.out.println("                彻底结束了"+System.currentTimeMillis());
        }
    });

    public void testMethod(){
        System.out.println(Thread.currentThread().getName()+" 准备！"+System.currentTimeMillis());
        try {
            if(Thread.currentThread().getName().equalsIgnoreCase("C")){
                Thread.sleep(Integer.MAX_VALUE);
            }
            cyclicBarrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" 开始！"+System.currentTimeMillis());
    }
}

class RunDemo123{
    public static void main(String[] args) {
        ColdService coldService = new ColdService();

        new Thread(() -> coldService.testMethod(),"A").start();

        new Thread(() -> coldService.testMethod(),"B").start();

        new Thread(() -> coldService.testMethod(),"C").start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("屏障对象的parties的个数为："+coldService.cyclicBarrier.getParties());
        System.out.println("在屏障处等待线程的个数为："+coldService.cyclicBarrier.getNumberWaiting());
    }
}
public class CyclicBarrierTimeOut {

    public static void main(String[] args) {
        BarrierTimeOutService barrierTimeOutService = new BarrierTimeOutService();

        new Thread(barrierTimeOutService::testMethod).start();

        new Thread(barrierTimeOutService::testMethod).start();
    }
}

class BarrierTimeOutService {

    public CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
        @Override
        public void run() {
            System.out.println("彻底结束了 " + System.currentTimeMillis());
        }
    });

    public void testMethod() {
        try {
            System.out.println(Thread.currentThread().getName() + " 准备！" + System.currentTimeMillis());
            if (Thread.currentThread().getName().equalsIgnoreCase("Thread-0")) {
                System.out.println("Thread-0 执行了 cyclicBarrier.await(5,TimeUnit.SECONDS)");
                cyclicBarrier.await(5, TimeUnit.SECONDS);
            }
            if (Thread.currentThread().getName().equalsIgnoreCase("Thread-1")) {
                System.out.println("Thread-0 执行了 cyclicBarrier.await()");
                cyclicBarrier.await();
            }
            System.out.println(Thread.currentThread().getName()+" 开始！"+System.currentTimeMillis());
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName()+" 进入catch (InterruptedException e)");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println(Thread.currentThread().getName()+" 进入catch (BrokenBarrierException e)");
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println(Thread.currentThread().getName()+" 进入catch (TimeoutException e)");
            e.printStackTrace();
        }

    }
}
